using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using Microsoft.Surface;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;

namespace Surfium
{
    /// <summary>
    /// Interaction logic for MySurfaceWindow.xaml
    /// </summary>
    public partial class MySurfaceWindow : SurfaceWindow
    {
        /// <summary>
        /// Default constructor.
        /// </summary>
        public MySurfaceWindow()
        {
            InitializeComponent();
            ContactHoldGesture += new ContactEventHandler(MySurfaceWindow_ContactHoldGesture);

            // Add handlers for Application activation events
            AddActivationHandlers();

        }

        void MySurfaceWindow_ContactHoldGesture(object sender, ContactEventArgs e)
        {
            if (e.OriginalSource == BackingGrid)
            {
                MyWindow mw = new MyWindow();
                mw.Center = e.Contact.GetCenterPosition(scatterView);
                mw.Orientation = e.Contact.GetOrientation(scatterView) + 90d;
                scatterView.Items.Add(mw);
                mw.RaiseAddedEvent();
            }
        }

    

        /// <summary>
        /// Occurs when the window is about to close. 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            // Remove handlers for Application activation events
            RemoveActivationHandlers();
        }

        /// <summary>
        /// Adds handlers for Application activation events.
        /// </summary>
        private void AddActivationHandlers()
        {
            // Subscribe to surface application activation events
            ApplicationLauncher.ApplicationActivated += OnApplicationActivated;
            ApplicationLauncher.ApplicationPreviewed += OnApplicationPreviewed;
            ApplicationLauncher.ApplicationDeactivated += OnApplicationDeactivated;
        }

        /// <summary>
        /// Removes handlers for Application activation events.
        /// </summary>
        private void RemoveActivationHandlers()
        {
            // Unsubscribe from surface application activation events
            ApplicationLauncher.ApplicationActivated -= OnApplicationActivated;
            ApplicationLauncher.ApplicationPreviewed -= OnApplicationPreviewed;
            ApplicationLauncher.ApplicationDeactivated -= OnApplicationDeactivated;
        }

        /// <summary>
        /// This is called when application has been activated.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnApplicationActivated(object sender, EventArgs e)
        {
            //TODO: enable audio, animations here
        }

        /// <summary>
        /// This is called when application is in preview mode.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnApplicationPreviewed(object sender, EventArgs e)
        {
            //TODO: Disable audio here if it is enabled

            //TODO: optionally enable animations here
        }

        /// <summary>
        ///  This is called when application has been deactivated.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnApplicationDeactivated(object sender, EventArgs e)
        {
            //TODO: disable audio, animations here
        }

        private void scatterViewItem_DragEnter(object sender, SurfaceDragDropEventArgs e)
        {
            MyDragCursor mdc = e.Cursor.Visual as MyDragCursor;

            mdc.RaiseDroppableNewEnterEvent();


        }

        private void scatterViewItem_DragLeave(object sender, SurfaceDragDropEventArgs e)
        {
            MyDragCursor mdc = e.Cursor.Visual as MyDragCursor;
            mdc.RaiseDroppableNewLeaveEvent();


        }

        private void Grid_Drop(object sender, SurfaceDragDropEventArgs e)
        {
            MyDragCursor mdc = e.Cursor.Visual as MyDragCursor;
            MyWindow mw = new MyWindow(mdc.Link);
            mw.Center = e.Cursor.GetPosition(scatterView);
            mw.Orientation = e.Cursor.GetOrientation(scatterView);
            scatterView.Items.Add(mw);
            mw.RaiseAddedEvent();
            
        }

       

    }
}